package top.kaoshanji.q1ch07a.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import top.kaoshanji.q1ch07a.model.Spitter;
import top.kaoshanji.q1ch07a.vo.SpitterForm;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/25 11:35
 */
@Controller
@RequestMapping("/spitter2")
public class SpitterController2 {

    @RequestMapping(value="/register", method= RequestMethod.GET)
    public String showRegistrationForm(Model model) {
        model.addAttribute("spitter" ,new Spitter());
        return "registerFormPage";
    }

    /**
     * 代码清单-q1ch07a-222页a：使用flash属性
     * @param spitterForm SpitterForm
     * @param model Model
     * @return String
     */
    @RequestMapping(value="/register", method= RequestMethod.POST)
    public String processRegistration(SpitterForm spitterForm, RedirectAttributes model) {

        Spitter spitter = spitterForm.toSpitter();

        // 使用 RedirectAttributes 参数设置...现在是对象了..
        model.addAttribute("userName", spitter.getUsername());
        // model.addAttribute("spitter", spitter); // 查询参数

        model.addFlashAttribute("spitter", spitter); // 这是个对象，需要更换一个方法
        return "redirect:/spitter2/spitterDetail/{userName}"; // 路径变量
    }

    @RequestMapping(value="/spitterDetail/{userName}", method=RequestMethod.GET)
    public String showSpitterProfile(@PathVariable("userName") String userName, Model model) {

        if (model.containsAttribute("spitter")) {
            System.out.println("...我是RedirectAttributes里取出来的...");
        }

        return "profilePage";
    }




}
